package com.ecode.annotation.builder.local;

import com.ecode.annotation.EcodeDo;
import com.ecode.annotation.ErrorCode;
import com.ecode.annotation.core.CodeDataPullBuilder;
import com.ecode.annotation.model.ErrorCodeDO;

import java.io.*;
import java.util.HashSet;
import java.util.Set;

/**
 * @ClassName LocalPathCodeDataPullBuilder
 * @Author mjlft
 * @Date 2021/10/4 11:59
 * @Version 1.0
 * @Description 本地数据拉取构建器
 */
public class LocalPathCodeDataPullBuilder implements CodeDataPullBuilder {

    @Override
    public String dataPull(EcodeDo ecodeDo) {
        String filePath = ecodeDo.getLocalPath();
        if (filePath == null || "".equals(filePath.trim())) {
            throw new RuntimeException("localPath必须配置");
        }
        File file = new File(filePath);
        BufferedReader bufferedReader = null;
        Set<ErrorCodeDO> errorCodeDOS = new HashSet<>();
        StringBuffer stringBuffer = new StringBuffer();
        try {
            bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
            String tmpStr = null;
            while ((tmpStr = bufferedReader.readLine()) != null) {
                stringBuffer.append(tmpStr.trim() + "\n");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return stringBuffer.toString();
    }
}
